﻿#include "stdafx.h"
#include "W_FieldProbe.h"

#include "Source/ModelingPreModule/S_ModelingPre_InteractiveManager.h"
#include "Source/MonitorModule/S_MonitorDataContainer.h"
#include "Source/MonitorModule/S_MonitorDisplayingManager.h"
#include "Source/ResultModule/S_ResultWindowManager.h"
#include "Source/MainModule/Global/S_TabOrderManager.h"
#include "Source/Utils/Common/TTool.h"
/*
-----==========================================================-----
		类：		场强探头 窗口类.h
		作者：		drill_up
		所属模块：	场强探头模块
		功能：		场强探头模块的窗口类对象
-----==========================================================-----
*/
W_FieldProbe::W_FieldProbe(QWidget *parent) :
	W_DialogBase(parent),
	ui(new Ui::W_FieldProbe),
	m_ButtonGroup(new QButtonGroup(this))
{
	ui->setupUi(this);

	//-----------------------------------
	//----参数初始化
	this->m_slotBlock_model = false;

	//-----------------------------------
	//----ui初始化
	m_ButtonGroup->addButton(ui->radioButton_x, 0);
	m_ButtonGroup->addButton(ui->radioButton_y, 1);
	m_ButtonGroup->addButton(ui->radioButton_z, 2);
	m_ButtonGroup->addButton(ui->radioButton_all, 3);

	ui->comboBox_coordinate->setDisabled(true);
	ui->label_coordinate->setDisabled(true);

	// > 类型
	ui->comboBox_field->addItems(C_FieldProbe::getFieldItems());
	ui->comboBox_field->setCurrentIndex(m_FieldProbe.m_FieldType);
	
	// > 类型 - 选项远场，第3、4项不可用）
	#ifdef DSEMUI_Publish_Setting_20240327
		ui->comboBox_field->setItemData(2, QVariant(0), Qt::UserRole - 1);
		ui->comboBox_field->setItemData(3, QVariant(0), Qt::UserRole - 1);
	#endif

	QString tmpName = QString("%1 (%2 %3 %4)").arg(ui->comboBox_field->itemText(m_FieldProbe.m_FieldType)).arg(m_FieldProbe.m_PosX).arg(m_FieldProbe.m_PosY).arg(m_FieldProbe.m_PosZ);
	m_FieldProbe.setName(tmpName);
	ui->lineEdit_name->setText(m_FieldProbe.getName());
	ui->checkBox_auto->setCheckState(m_FieldProbe.m_autoCheck ? Qt::Checked : Qt::Unchecked);

	// > 窗口初始化
	this->setDataInAddMode();
	this->initDialogButtonBox(ui->buttonBox);

	//-----------------------------------
	//----事件绑定
	connect(ui->checkBox_auto, &QCheckBox::stateChanged, this, &W_FieldProbe::onRefreshCheckToData);
	connect(m_ButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &W_FieldProbe::onRefreshRadioToData);

	connect(ui->comboBox_field, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &W_FieldProbe::onRefreshNameToUi);
	connect(ui->checkBox_auto, &QCheckBox::stateChanged, this, &W_FieldProbe::onRefreshNameToUi);
	connect(ui->lineEdit_x, &QLineEdit::textChanged, this, &W_FieldProbe::onRefreshNameToUi);
	connect(ui->lineEdit_y, &QLineEdit::textChanged, this, &W_FieldProbe::onRefreshNameToUi);
	connect(ui->lineEdit_z, &QLineEdit::textChanged, this, &W_FieldProbe::onRefreshNameToUi);

	// > 事件绑定 - 模型预览
	connect(ui->lineEdit_x, &QLineEdit::textEdited, this, &W_FieldProbe::refreshModel);
	connect(ui->lineEdit_y, &QLineEdit::textEdited, this, &W_FieldProbe::refreshModel);
	connect(ui->lineEdit_z, &QLineEdit::textEdited, this, &W_FieldProbe::refreshModel);
	connect(m_ButtonGroup, QOverload<int>::of(&QButtonGroup::buttonClicked), this, &W_FieldProbe::refreshModel);

	//-----------------------------------
	//----配置tab顺序
	S_TabOrderManager::getInstance()->configureTabOrder(this);
}

W_FieldProbe::~W_FieldProbe()
{
	delete ui;
}

Ui::W_FieldProbe *W_FieldProbe::getUI()
{
	return ui;
}

/*-------------------------------------------------
	控件 - 生成name
*/
void W_FieldProbe::onRefreshNameToUi() {
	if (m_FieldProbe.m_autoCheck) {
		// 获取ui->comboBox_field ui->lineEdit_x ui->lineEdit_y ui->lineEdit_z;控件中的内容生成name
		double posX = 0;
		double posY = 0;
		double posZ = 0;
		QString fieldType = "";

		// 获取 QLineEdit 列表
		QList<QLineEdit *> lineEditList = ui->widget_xyz->findChildren<QLineEdit *>();

		// 遍历 QLineEdit 列表
		foreach(QLineEdit *lineEdit, lineEditList) {
			// 获取 QLineEdit 的名称
			QString name = lineEdit->objectName();

			// 根据名称获取相应的临时变量，并将 QLineEdit 的 text 赋值给该临时变量
			if (name == "lineEdit_x") {
				posX = ui->lineEdit_x->text().toDouble();
			}
			else if (name == "lineEdit_y") {
				posY = ui->lineEdit_y->text().toDouble();
			}
			else if (name == "lineEdit_z") {
				posZ = ui->lineEdit_z->text().toDouble();
			}
		}

		fieldType = ui->comboBox_field->currentText();

		QString tmpName = QString("%1 (%2 %3 %4)").arg(fieldType).arg(posX).arg(posY).arg(posZ);
		ui->lineEdit_name->setText(tmpName);

		// todo 这里设置名称是否有问题
		m_FieldProbe.setName(tmpName);
	}
}


/*-------------------------------------------------
		控件 - 点击勾选
*/
void W_FieldProbe::onRefreshCheckToData(int state) {
	m_FieldProbe.m_autoCheck = (state == Qt::Checked);
}
/*-------------------------------------------------
		控件 - 点击单选
*/
void W_FieldProbe::onRefreshRadioToData(int id) {
	m_FieldProbe.m_OrientationType = id;
}
/*-------------------------------------------------
		控件 - 设置点
*/
void W_FieldProbe::setDataPoint(QList<double> p1) {
	if (p1.count() < 3) { return; }
	ui->lineEdit_x->setText(QString::number(p1.at(0)));
	ui->lineEdit_y->setText(QString::number(p1.at(1)));
	ui->lineEdit_z->setText(QString::number(p1.at(2)));
	this->refreshModel();
}



/*-------------------------------------------------
		模型 - 刷新模型
*/
void W_FieldProbe::refreshModel() {
	if (this->m_slotBlock_model == true) { return; }

	// > 若已存在，则删除
	this->clearModel();


	// > 临时创建
	//		声明一个长度为 3 的数组，用于存储需要设置为 1 的变量
	int xyz[3] = { 0, 0, 0 };

	// 根据 checkedId 来设置数组中对应的变量
	int checkedId = m_ButtonGroup->checkedId();
	if (checkedId == 0 || checkedId == 3) {
		// 如果 checkedId 为 0 或 3，则设置 x 变量为 1
		xyz[0] = 1;
	}
	if (checkedId == 1 || checkedId == 3) {
		// 如果 checkedId 为 1 或 3，则设置 y 变量为 1
		xyz[1] = 1;
	}
	if (checkedId == 2 || checkedId == 3) {
		// 如果 checkedId 为 2 或 3，则设置 z 变量为 1
		xyz[2] = 1;
	}

	// 遍历数组中的每个元素
	for (int i = 0; i < 3; i++) {
		if (xyz[i] == 1) {
			// 如果该元素为 1，则依次创建对应的 FieldProbe 实例并分别赋值给 m_model_idx、m_model_idy、m_model_idz 变量
			double x = ui->lineEdit_x->text().toDouble(); // 获取 x 坐标值
			double y = ui->lineEdit_y->text().toDouble(); // 获取 y 坐标值
			double z = ui->lineEdit_z->text().toDouble(); // 获取 z 坐标值

			QColor color(Qt::green);
			if (i == 0) {
				// 如果 i 为 0，则创建 x 方向的 FieldProbe 实例
				m_model_idx = S_ModelingPre_InteractiveManager::getInstance()->model_CreateFieldProbe(
					x, y, z,
					1, 0, 0, color, false
				);
			}
			if (i == 1) {
				// 如果 i 为 1，则创建 y 方向的 FieldProbe 实例
				m_model_idy = S_ModelingPre_InteractiveManager::getInstance()->model_CreateFieldProbe(
					x, y, z,
					0, 1, 0, color, false
				);
			}
			if (i == 2) {
				// 如果 i 为 2，则创建 z 方向的 FieldProbe 实例
				m_model_idz = S_ModelingPre_InteractiveManager::getInstance()->model_CreateFieldProbe(
					x, y, z,
					0, 0, 1, color, false
				);
			}
		}
	}
	S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();
	
}
/*-------------------------------------------------
		模型 - 删除模型
*/
void W_FieldProbe::clearModel() {
	if (this->m_model_idx != "") {
		S_ModelingPre_InteractiveManager::getInstance()->model_destory(this->m_model_idx);
	}
	if (this->m_model_idy != "") {
		S_ModelingPre_InteractiveManager::getInstance()->model_destory(this->m_model_idy);
	}
	if (this->m_model_idz != "") {
		S_ModelingPre_InteractiveManager::getInstance()->model_destory(this->m_model_idz);
	}
}


/*-------------------------------------------------
		窗口 - 设置数据（添加）
*/
void W_FieldProbe::setDataInAddMode() {
	this->m_FieldProbe.init();
	this->isAddMode = true;
	this->putDataToUi(); 
}
/*-------------------------------------------------
		窗口 - 设置数据（修改）
*/
void W_FieldProbe::setDataInModifyMode(C_FieldProbe p) {
	this->m_FieldProbe = p;
	this->isAddMode = false;
	this->putDataToUi(); 
}
/*-------------------------------------------------
		窗口 - 取出数据
*/
C_FieldProbe W_FieldProbe::getData() {
	return this->m_FieldProbe;
};
/*-------------------------------------------------
		窗口 - 本地数据 -> ui数据
*/
void W_FieldProbe::putDataToUi() {
	this->m_slotBlock_model = true;

	ui->lineEdit_name->setText(m_FieldProbe.getName());
	ui->checkBox_auto->setChecked(m_FieldProbe.m_autoCheck ? Qt::Checked : Qt::Unchecked);
	ui->comboBox_field->setCurrentIndex(m_FieldProbe.m_FieldType);

	// 单选组
	m_ButtonGroup->button(m_FieldProbe.m_OrientationType)->setChecked(true);

	TTool::_double_(ui->lineEdit_x, &this->m_FieldProbe.m_PosX);
	TTool::_double_(ui->lineEdit_y, &this->m_FieldProbe.m_PosY);
	TTool::_double_(ui->lineEdit_z, &this->m_FieldProbe.m_PosZ);

	this->m_slotBlock_model = false;
	this->refreshModel();
};
/*-------------------------------------------------
		窗口 - ui数据 -> 本地数据
*/
void W_FieldProbe::putUiToData() {
	bool mAutoCheck;
	int mFieldType;				
	int mOrientationType;			//方向 (0:X, 1:Y, 2:Z, 3:All(X, Y, Z, Abs)
	double mPosX;
	double mPosY;
	double mPosZ;

	m_FieldProbe.setName(ui->lineEdit_name->text());
	m_FieldProbe.m_autoCheck = ui->checkBox_auto->isChecked();
	m_FieldProbe.m_FieldType = ui->comboBox_field->currentIndex();

	// todo radiobutton 在匿名函数中， 需要集成
	TTool::_double_(&m_FieldProbe.m_PosX, ui->lineEdit_x);
	TTool::_double_(&m_FieldProbe.m_PosY, ui->lineEdit_y);
	TTool::_double_(&m_FieldProbe.m_PosZ, ui->lineEdit_z);

	// qDebug() << ui->lineEdit_name->text() << ui->checkBox_auto->isChecked() << ui->comboBox_field->currentIndex();
	// qDebug() << ui->lineEdit_x->text() << ui->lineEdit_y->text() << ui->lineEdit_z->text();
};

/*-------------------------------------------------
		窗口 - 提交数据（校验）
*/
void W_FieldProbe::base_acceptData() {
	this->putUiToData();

	// > 校验 - 非空
	if (m_FieldProbe.getName().trimmed() == "") {
		QMessageBox::warning(this, "提示", "场强探头名称不能为空。");
		return;
	}

	// > 校验 - 操作是否与结果文件冲突（含迁移处理）
	if (S_ResultWindowManager::getInstance()->isConflictToResult_WithMigrateTip()) {
		return;
	}

	emit acceptDataSignal(this->isAddMode, this->m_FieldProbe);
	W_DialogBase::base_acceptData();

	// > 清理模型
	this->clearModel();
};

/*-------------------------------------------------
		窗口 - 取消数据（校验）
*/
void W_FieldProbe::base_rejectData() {
	W_DialogBase::base_rejectData();

	// > 清理模型
	this->clearModel();

	// > 重新显示旧模型
	if (this->isAddMode == false) { 
		S_MonitorDisplayingManager::getInstance()->refreshModel_NoUpdate_Probe(this->m_FieldProbe.getId());
		S_ModelingPre_InteractiveManager::getInstance()->updateViewModeling();
	}
}

/*-------------------------------------------------
		窗口 - 关闭事件
*/
void W_FieldProbe::closeEvent(QCloseEvent *e) {
	W_DialogBase::closeEvent(e);

	// > 清理模型
	this->clearModel();
}